<?php

$GLOBALS['THRIFT_ROOT'] = LIB_PATH . "/thrift";
require_once( $GLOBALS['THRIFT_ROOT'].'/Thrift.php' );
require_once( $GLOBALS['THRIFT_ROOT'].'/transport/TSocket.php' );
require_once( $GLOBALS['THRIFT_ROOT'].'/transport/TBufferedTransport.php' );
require_once( $GLOBALS['THRIFT_ROOT'].'/protocol/TBinaryProtocol.php' );
require_once( $GLOBALS['THRIFT_ROOT'].'/packages/Hbase/Hbase.php' );

class HbaseQueryer {
	
	private $socket;
	private $transport;
	private $client;
	private $protocol;
	
	public function connect($hbaseHost, $hbasePort) {
		try{
			//$socket = new TSocket( 'w-hdp2.safe.zzbc.qihoo.net', 9090);
			$this->socket = new TSocket($hbaseHost, $hbasePort);
			$this->socket->setSendTimeout( 1000 ); // One seconds
			$this->socket->setRecvTimeout( 20000 ); // Twenty seconds
			$this->transport = new TBufferedTransport( $socket );
			$this->protocol = new TBinaryProtocol( $transport );
			
			$this->client = new ThriftHiveClient( $protocol );			
			$this->transport->open();
		}
		catch(Exception $e){
			exit("connect to hbase error!");
		}
	}
	
	
	public function query($hiveQL) {
		
		try {
			$client->execute($hiveQL);
			
			// can do like this?
			$client->fetchAll();
		} 
		catch ( Exception $nf ) {
			//  $client->scannerClose( $scanner );
			// echo( "Scanner finished\n" );
		}
	}
	
	public function close() {
		$this->transport->close();
	}

}